package com.devmindset.legoframework.usage.client.component;

import com.devmindset.legoframework.client.mvc.LegoComponent;
import com.devmindset.legoframework.client.mvc.LegoEvent;
import com.devmindset.legoframework.client.mvc.LegoModel;
import com.devmindset.legoframework.client.mvc.metadata.ComponentStory;
import com.devmindset.legoframework.client.mvc.metadata.Stories;
import com.devmindset.legoframework.client.screen.ApplicationScreenManager;
import com.devmindset.legoframework.usage.client.event.Events;
import com.devmindset.legoframework.usage.client.screen.OrganizerScreen;
import com.devmindset.legoframework.usage.client.text.UsageTexts;
import com.smartgwt.client.types.LayoutPolicy;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;

public class NavigationComponent extends LegoComponent {
	
	private SectionStack sectionStack;
	
	/** {@inheritDoc} */	
	@Override
	public Canvas construct(LegoModel model) {
		//initialize
		sectionStack = new SectionStack();
		sectionStack.setBackgroundColor("#EDEDED");
		sectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
		sectionStack.setWidth100();
		sectionStack.setHeight100();
		sectionStack.setAnimateMembers(true);
		sectionStack.setVPolicy(LayoutPolicy.FILL);
		
		SectionStackSection home = new SectionStackSection(UsageTexts.get().home());
		home.setExpanded(true);
		home.setResizeable(false);
		
		IButton overview = new IButton();
		overview.setWidth100();
		overview.setTitle("<span class=\"navLabel\">" + UsageTexts.get().overview() + "</span>");
		overview.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				broadcastEvent(new LegoEvent(Events.RENDER_OVERVIEW_COMPONENT));
			}
		});
		home.addItem(overview);
		
		SectionStackSection myActivities = new SectionStackSection(UsageTexts.get().myActivities());
		myActivities.setExpanded(true);
		myActivities.setResizeable(false);
		//pending orders
		IButton pendingOrders = new IButton();
		pendingOrders.setWidth100();
		pendingOrders.setTitle("<span class=\"navLabel\">" + UsageTexts.get().pendingOrders() + "</span>");
		pendingOrders.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				broadcastEvent(new LegoEvent(Events.RENDER_PENDING_ORDERS_COMPONENT));
			}
		});
		myActivities.addItem(pendingOrders);
		
		//lookup country data
		IButton lookupCountryData = new IButton();
		lookupCountryData.setWidth100();
		lookupCountryData.setTitle("<span class=\"navLabel\">" + UsageTexts.get().lookupCountryData() + "</span>");
		lookupCountryData.addClickHandler(new ClickHandler() {			
			public void onClick(ClickEvent event) {
				broadcastEvent(new LegoEvent(Events.RENDER_COUNTRY_DETAILS_COMPONENT));
			}
		});
		myActivities.addItem(lookupCountryData);
		
		SectionStackSection tools = new SectionStackSection(UsageTexts.get().tools());
		tools.setExpanded(true);
		tools.setResizeable(false);
		
		IButton organizer = new IButton();
		organizer.setWidth100();
		organizer.setTitle("<span class=\"navLabel\">" + UsageTexts.get().organizer() + "</span>");
		organizer.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ApplicationScreenManager.activate(OrganizerScreen.class);
			}
		});
		tools.addItem(organizer);
		
		sectionStack.addSection(home);
		sectionStack.addSection(myActivities);
		sectionStack.addSection(tools);
		
		return sectionStack;
	}

	/** {@inheritDoc} */	
	@Override
	public void calculate(LegoModel model) {
		//one-time construction component; nothing to calculate for myself.
	}
	
	/** {@inheritDoc} */	
	public ComponentStory myStory() {
		return Stories.component()
			.constructedBy(Events.RENDER_NAVIGATION_COMPONENT)
			.knownAs("Navigation Component");
	}

}